package com.application.goods.listener;

import com.application.goods.service.GoodsHtmlService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 【RabbitMQ 监听】
 * RabbitMQ 的 xxacker.items.exchange 的监听者
 * @author Xxacker
 */
@Component
public class GoodsListener {

    @Autowired
    private GoodsHtmlService goodsHtmlService;

    /**
     * 用来监听保存的方法
     * @param id
     * @throws Exception
     * @Queue value   队列的名称
     *        durable 是否持久化队列
     * @Exchange value                         交换机的名称
     *           ignoreDeclarationExceptions   是否忽略声明异常
     *           type                          类型
     * key：String数组，里面可以定义多个key
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "EXCELLENT.CREATE.WEB.QUEUE", durable = "true"),
            exchange = @Exchange(
                    value = "EXCELLENT.ITEM.EXCHANGE",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
            key = {"item.insert", "item.update"}))
    public void save(Long id) throws Exception {
        if (id == null) {
            return;
        }
        // 创建页面
        goodsHtmlService.createHtml(id);
    }

    /**
     * 用来监听删除方法
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "EXCELLENT.DELETE.WEB.QUEUE", durable = "true"),
            exchange = @Exchange(
                    value = "EXCELLENT.ITEM.EXCHANGE",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
            key = "item.delete"))
    public void listenDelete(Long id) {
        if (id == null) {
            return;
        }
        // 删除页面
        goodsHtmlService.deleteHtml(id);
    }

}
